package com.ruoyi.web.controller.hospitals;


import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.hospital.service.SchedulesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hospital.domain.Schedules;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排班信息Controller
 *
 * @author xiaozhao
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/schedules/schedules")
public class SchedulesController extends BaseController
{
    @Autowired
    private SchedulesService schedulesService;

    /**
     * 查询排班信息列表
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:list')")
    @GetMapping("/list")
    public TableDataInfo list(Schedules schedules)
    {
        startPage();
        List<Schedules> list = schedulesService.selectSchedulesList(schedules);
        return getDataTable(list);
    }

    /**
     * 导出排班信息列表
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:export')")
    @Log(title = "排班信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Schedules schedules)
    {
        List<Schedules> list = schedulesService.selectSchedulesList(schedules);
        ExcelUtil<Schedules> util = new ExcelUtil<Schedules>(Schedules.class);
        util.exportExcel(response, list, "排班信息数据");
    }

    /**
     * 获取排班信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:query')")
    @GetMapping(value = "/{scheduleId}")
    public AjaxResult getInfo(@PathVariable("scheduleId") Long scheduleId)
    {
        return success(schedulesService.selectSchedulesByScheduleId(scheduleId));
    }

    /**
     * 新增排班信息
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:add')")
    @Log(title = "排班信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Schedules schedules)
    {
        return toAjax(schedulesService.insertSchedules(schedules));
    }

    /**
     * 修改排班信息
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:edit')")
    @Log(title = "排班信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Schedules schedules)
    {
        return toAjax(schedulesService.updateSchedules(schedules));
    }

    /**
     * 删除排班信息
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:remove')")
    @Log(title = "排班信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scheduleIds}")
    public AjaxResult remove(@PathVariable Long[] scheduleIds)
    {
        return toAjax(schedulesService.deleteSchedulesByScheduleIds(scheduleIds));
    }

    /**
     * 更新号源数量（新增方法）
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:edit')")
    @Log(title = "排班信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateQuota")
    public AjaxResult updateQuota(
            @RequestParam Long doctorId,
            @RequestParam String appointmentDate, // 建议使用String接收日期
            @RequestParam String timeSlot,
            @RequestParam int change
    ) {
        try {
            // 将String日期转换为Date（使用DateUtils工具类）
            Date date = com.ruoyi.common.utils.DateUtils.parseDate(appointmentDate);
            int result = schedulesService.updateQuota(doctorId, date, timeSlot, change);
            return result > 0 ? success("更新成功") : error("更新失败");
        } catch (Exception e) {
            return error("更新失败：" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('shcedules:schedules:list')")
    @GetMapping("/getAllSchedules")
    public AjaxResult getAllSchedules(Schedules schedules)
    {
        List<Schedules> schedulesList = schedulesService.selectSchedulesList(schedules);
        return success(schedulesList);
    }

    @PreAuthorize("@ss.hasPermi('shcedules:schedules:list')")
    @GetMapping("/getDoctorSchedules")
    public AjaxResult getDoctorSchedules(@RequestParam Long doctorId)
    {
        Schedules schedules = new Schedules();
        schedules.setDoctorId(doctorId);
        List<Schedules> schedulesList = schedulesService.selectSchedulesList(schedules);
        return success(schedulesList);
    }

    /**
     * 根据科室ID和就诊日期查询该科室医生的值班信息
     */
    @PreAuthorize("@ss.hasPermi('schedules:schedules:list')")
    @GetMapping("/getSchedulesByDeptAndDate")
    public AjaxResult getSchedulesByDeptAndDate(@RequestParam Long deptId, @RequestParam Date appointmentDate) {
        List<Schedules> schedulesList = schedulesService.selectSchedulesByDeptIdAndDate(deptId, appointmentDate);
        return success(schedulesList);
    }

}

